local balancer = require "ngx.balancer"
local upstream = require "ngx.upstream"

-- 加载cjson
local cjson = require("cjson");

local cache = ngx.shared.cache

--读取get参数
--local uri_args = ngx.req.get_uri_args()
--读取post参数
ngx.req.read_body();

--local uri_args = ngx.req.get_post_args()
local data = ngx.req.get_body_data(); --获取消息体

--ngx.say(data)

local restOut = { respCode = 0, resp_msg = "操作成功", datas = {} };
local errorOut = { respCode = -1, resp_msg = "操作失败", datas = {} };


ngx.log(ngx.DEBUG,"data=" .. data);

local args=cjson.decode(data);
ngx.log(ngx.DEBUG,"args=" .. tostring(args));

local serverCount =args["serverCount"];
ngx.log(ngx.DEBUG,"serverCount=" .. tostring(serverCount));

if not serverCount or serverCount == ngx.null then
    errorOut.resp_msg="serverCount 为空！";
    ngx.say(cjson.encode(errorOut));
    return ;
end

local iServerCount = tonumber(serverCount)-1;
ngx.log(ngx.DEBUG,"iServerCount=" .. iServerCount);

for i = 0,iServerCount do
    cache:set(i, args["server"..tostring(i)])
    ngx.log(ngx.DEBUG,"i=" .. args["server"..tostring(i)])

end
cache:set("serverCount",tonumber(serverCount));


restOut.datas = "更新的server数："..serverCount;
ngx.say(cjson.encode(restOut));
